from dask.distributed import Client, LocalCluster
import numpy as np
import pandas as pd
import dask.dataframe as dd


def inc(x):
   return x + 1

def add(x, y):
   return x + y


def sub100(x):
    index = pd.date_range("2021-09-01", periods=2400, freq="1h")
    df = pd.DataFrame({"a": np.arange(2400), "b": list("abcaddbe" * 300)}, index=index)
    ddf = dd.from_pandas(df, npartitions=2)
    result = ddf["2021-10-01": "2021-10-09 5:00"].a.cumsum() - 100
    return result

if __name__ == "__main__":
    # cluster = LocalCluster("127.0.0.1:8786", workers=8, threads=8)

    # client = Client()
    # client = Client(processes=False)  # start local workers as threads

    # The Client() call described above is shorthand for creating a LocalCluster and then passing that to your client.
    # This is equivalent, but somewhat more explicit.
    cluster = LocalCluster()
    client = Client(cluster)
    a = client.submit(inc, 1)     # work starts immediately
    b = client.submit(inc, 2)     # work starts immediately
    c = client.submit(add, a, b)  # work starts immediately
    sub100Result =client.submit(sub100,c);
    print(sub100Result.result().compute())
    client.close()
